﻿<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Web;
using System.Data;
using System.Data.Common;
using System.Web.Configuration;
using System.Data.OracleClient;
using System.Data.OleDb;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Maticsoft.DBUtility;
using System.Web;

/*
 * 处理程序
 * 实现直接读取数据库中的图像而从路径读取
 */ 

public class ImageHandler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/jpeg";//设置返回的数据类型
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.BufferOutput = false;
        OracleDB db = new OracleDB();
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        string photoID = context.Request.QueryString["PhotoID"];
        //判断是否要显示大图，
        string IsBigPhoto = context.Request.QueryString["IsBigPhoto"];
        if (photoID == null)
            throw new ApplicationException("Must specify ID");
        OracleConnection connection = new OracleConnection(connectionString);
        string sql = "SELECT filepath||uniqueno||'_'||filename as path FROM TAB_FILESDB WHERE AUTOID=:AUTOID";//从数据库中读取二进制数据流 '10633442183190431250'";
        OracleCommand command = new OracleCommand(sql, connection);
        command.Parameters.AddWithValue("AUTOID", photoID);
        try
        {
            connection.Open();
            OracleDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);//使用datareader保存数据
            reader.Read();

            string realPath = HttpContext.Current.Server.MapPath(reader["path"].ToString());

            if (File.Exists(realPath))
            {
                byte[] bytes = File.ReadAllBytes(realPath);
                System.IO.MemoryStream ms = new MemoryStream(bytes);//二进制数据放入内存中
                System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);//创建图像
                //如果不是显示大图则传回缩略图，减少数据的传输量
                if (IsBigPhoto == null)
                {
                    image = image.GetThumbnailImage(127, 128, null, System.IntPtr.Zero);//设置图像大小
                }
                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);//设置图像保存方式,在这里已经将图像作为返回的数据
                image.Dispose();//释放资源
                ms.Dispose();
                /* 以下也可读取图像,但很慢,而且不能先控制图像大小再输出,只能在页面设置
                 * 与上一种方式相比,改变大小后图像的显示质量十分差
                if (reader.Read()) {
                
                    int buffersize = 100;
                    byte[] bytes = new byte[buffersize];
                    long bytesRead;
                    long readFrom = 0;
                    do
                    {
                        bytesRead = reader.GetBytes(0, readFrom, bytes, 0, buffersize);
                        context.Response.BinaryWrite(bytes);
                        readFrom += buffersize;
                    } while (bytesRead == buffersize);
                }
                 * 
                 * */
            }
            reader.Close();
        }
        catch (OracleException oex)
        {
            context.Response.ContentType = "text/html";
            context.Response.Write("异常: " + oex.Message);
        }
        catch (InvalidOperationException ioe)
        {
            context.Response.ContentType = "text/html";
            context.Response.Write("异常: " + ioe.Message);
        }
        catch (Exception aoe)
        {
            context.Response.ContentType = "text/html";
            context.Response.Write("异常: " + aoe.Message + "。请重试！");
        }
        finally
        {
            connection.Close();
        }
    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }

}